//
//  main.m
//  OC04_homework
//
//  Created by Hanlingyu on 16/7/7.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Color.h"
int main(int argc, const char * argv[]) {
    // 处理⽂件crayons.txt中的⽂本信息，⽂本内容是关于颜⾊的，每⾏都是⼀ 个颜⾊的信息，例如：Almond #EED9C4，前⼀个字符串是颜⾊的名称，后⼀ 个字符串是颜⾊的16进制⾊值，处理⽂本完成如下需求:
    // 使⽤字典管理所有的颜⾊，即字典中存储的是多个键值对，颜⾊名称为 key，16进制颜⾊值（不带#）是value。
    // 使⽤⼀个新的字典管理颜⾊，对颜⾊进⾏分类管理，即：“A”，“B”，“C”… 即这个字典包含多个键值对，key是26个字⺟，value是数组，数组⾥存放的是 Color对象（包含name和colorValue）。需要⾃⼰创建Color类。

#pragma mark - 自己写法
#if 0
    NSString *fileStr = [NSString stringWithContentsOfFile:@"/Users/ltq/DLS160710/Objective-C/HomeWork/OC04_homework/OC04_homework/crayons.txt" encoding:NSUTF8StringEncoding error:nil];
    NSArray *lineArray = [fileStr componentsSeparatedByString:@"\n"];
    // 用来装value的字典
    NSMutableDictionary *colorValueDic = [NSMutableDictionary dictionary];
    // 用来装color对象的字典
    NSMutableDictionary *colorDic = [NSMutableDictionary dictionary];
    // 数据源中最后一行是空格, 所以lineArray.count - 1;
    for (int i = 0; i < lineArray.count - 1; i++) {
        NSLog(@"%d", i);
        NSString *colorStr = lineArray[i];
        NSString *divideChar = @"#";
        // 求出"#"的开始位置, 即为value的开始位置
        NSRange divideCharRange = [colorStr rangeOfString:divideChar];
        NSRange valueRange = {divideCharRange.location + 1, 6};
        // 求出colorValue的范围
        NSString *valueColor = [colorStr substringWithRange:valueRange];
        NSRange nameRange = {0, divideCharRange.location -1};
        NSString *nameColorStr = [colorStr substringWithRange:nameRange];
        // 创建color对象
        Color *color = [[Color alloc] initWithColorName:nameColorStr colorValue:valueColor];
        // 提取首字母, 作为key
        NSString *firstChar = [nameColorStr substringToIndex:1];
        [colorValueDic setObject:valueColor forKey:nameColorStr];
        // 将color对象装入字典
        NSMutableArray *array = [colorDic objectForKey:firstChar];
        if (array == nil) {
            array = [NSMutableArray array];
            [array addObject:color];
            [colorDic setObject:array forKey:firstChar];
        } else {
            [array addObject:color];
            [colorDic setObject:array forKey:firstChar];
        }
    }
    NSLog(@"%@", colorDic);
    NSLog(@"%@", colorValueDic);
#endif
    
#pragma mark - 老师写法
    
    NSString *path = @"/Users/ltq/DLS160710/Objective-C/HomeWork/OC04_homework/OC04_homework/crayons.txt";
    // 通过文件路径, 读取文件中的字符串
    NSString *sourceString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    // 根据\n切割字符串
    NSArray *colorStringArray = [sourceString componentsSeparatedByString:@"\n"];
    NSLog(@"%@", colorStringArray);
    // 声明存储颜色的可变字典
    NSMutableDictionary *colorDictionary = [NSMutableDictionary dictionary];
    for (int i = 0; i < colorStringArray.count; i++) {
        //筛选掉空的字符串
        if ([colorStringArray[i] length] > 0) {
            NSArray *color = [colorStringArray[i] componentsSeparatedByString:@" #"];
            [colorDictionary setObject:[color lastObject] forKey:[color firstObject]];
        }
    }
    NSLog(@"%@", colorDictionary);
    
    // 创建新字典
    NSMutableDictionary *newColorDictionary = [NSMutableDictionary dictionary];
    // 取到老字典中所有key的值
    NSArray *allKeys = colorDictionary.allKeys;
    for (int i = 0; i < colorDictionary.allKeys.count; i++) {
        // 取首字母
        NSString *subString = [allKeys[i] substringWithRange:NSMakeRange(0, 1)];
        // 通过首字母取出对应的Array
        NSMutableArray *colorArray = [newColorDictionary objectForKey:subString];
        // 根据老字典的key和value生成新的Color对象
        Color *newColor = [[Color alloc] init];
        newColor.name = allKeys[i];
        newColor.colorValue = [colorDictionary objectForKey:allKeys[i]];
        // 判断从新字典中取出的数组是否为空
        // 若为空, 说明还没有存储过对应首字母的数组
        if (colorArray == nil) {
            // 创建数组
            colorArray = [NSMutableArray arrayWithObject:newColor];
            //赋值给对应的首字母
            [newColorDictionary setObject:colorArray forKey:subString];
        } else {
            // 如果数组不为空, 直接添加
            [colorArray addObject:newColor];
        }
    }
    NSLog(@"%@", newColorDictionary);
    
    
    
    
    return 0;
}
